package com.album.activity.adapter;

 
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;  
import java.util.Vector;

import com.album.R;
import com.album.common.Constants;
import com.album.common.Sharevariable;
 

import android.content.Context;  
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.BaseAdapter;  
import android.widget.GridView;
import android.widget.ImageView;  
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;  
  
/** 
 * Copyright (C) 2010,Under the supervision of China Telecom Corporation Limited 
 * Guangdong Research Institute The New Vphone Project 
 *  
 * @Author fonter.yang 
 * @Create date：2010-10-11 
 *  
 */  
public class MyPhotoAdapter extends BaseAdapter {  
  
    private class ViewHolder {  
        ImageView appImage;   
    }  
  
    private Context context;  
    private LayoutInflater mInflater;  
    private int public_ = 0; 
 
    public MyPhotoAdapter(Context c,int public_) {   
        this.context = c;  
        this.mInflater = LayoutInflater.from(context);
        this.public_=public_;
    } 
    private List<Bitmap> getDataSource(int public_){
    	switch (public_) {
		case 0:
			return Sharevariable.listBitmap_public;
		case 1:
			return Sharevariable.listBitmap_pwd;
		case 2:
			return Sharevariable.listBitmap_share;
		default:
			break;
		}
    	return Sharevariable.listBitmap_public;
    }
    
    public void setListDataSetChanged(Bitmap bitmap,int public_){ 
    	if(bitmap!=null) 
    		switch (public_) {
			case 0:
				Sharevariable.listBitmap_public.add(bitmap);
				break;
			case 1:
				Sharevariable.listBitmap_pwd.add(bitmap);
				break;
			case 2:
				Sharevariable.listBitmap_share.add(bitmap);
				break;
			default:
				break;
			}
    		notifyDataSetChanged();
    	
    } 
    
    public int getCount() {  
        // TODO Auto-generated method stub  
        return (getDataSource(public_) !=null && getDataSource(public_).size()>0) ? getDataSource(public_).size():0;  
    }  
  
    @Override  
    public Object getItem(int position) {  
    	return (getDataSource(public_) !=null && getDataSource(public_).size()>0) ? getDataSource(public_).get(position): null; 
    }  
  
   
    @Override  
    public long getItemId(int position) {  
        return position;  
    }  
      
    @Override  
    public View getView(int index, View convertView, ViewGroup parent) { 
    	
    	ViewHolder holder;  
        if (convertView == null) {     
        	LayoutInflater inflater  = LayoutInflater.from(context);  
            convertView = inflater.inflate(R.layout.expandablegrid_item, null);     
            holder = new ViewHolder();  
            holder.appImage = (ImageView)convertView.findViewById(R.id.itemImage); 
            holder.appImage.setLayoutParams(new GridView.LayoutParams(Constants.m_screenWidth,Constants.m_screenWidth));//设置ImageView宽高
        	holder.appImage.setAdjustViewBounds(false); 
        	holder.appImage.setPadding(2, 3, 3, 1);
        	holder.appImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
            convertView.setTag(holder);     
        }else{  
             holder = (ViewHolder) convertView.getTag();
        }   
        Bitmap bitmap =  getDataSource(public_).get(index);
        try {
	        if(bitmap!=null){
	        	holder.appImage.setImageBitmap(bitmap);
	        }else{
	        	holder.appImage.setImageResource(R.drawable.icon);
	        }
		} catch (IndexOutOfBoundsException e) {
			e.printStackTrace(); return null;
		}
        return convertView;  
    }  
   
 
    public void resletDataSetChanged(){
		List<Bitmap> listBitmap = getDataSource(public_);
    	if(listBitmap!=null && listBitmap.size()>0)
    		for(Bitmap bitmap : listBitmap){ 
    			if(bitmap !=null && bitmap.isRecycled())
    				bitmap.recycle(); 
					bitmap=null;
    		}
    	listBitmap.clear();
    	listBitmap = null; 
    	notifyDataSetChanged();
    }

//	if (listBitmap != null && index < listBitmap.size() && listBitmap.get(index) != null
//			&& !listBitmap.get(index).get().isRecycled()) {
//		holder.appImage.setImageBitmap(listBitmap.get(index).get());
//	}
}  
